package com.newsnetwork.article.service.impl;

import com.github.pagehelper.PageHelper;
import com.newsnetwork.api.service.BaseService;
import com.newsnetwork.article.mapper.ArticleMapper;
import com.newsnetwork.article.service.ArticlePortalService;
import com.newsnetwork.enums.ArticleReviewStatus;
import com.newsnetwork.enums.YesOrNo;
import com.newsnetwork.pojo.Article;
import com.newsnetwork.pojo.vo.ArticleDetailVO;
import com.newsnetwork.utils.PagedGridResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author: HuangDekai
 * @date: 2022/11/12 13:27
 * @version: 1.0
 * @since: jdk8
 * @description: 门户端文章业务的 Service, 首页查询文章接口的实现
 */
@Service
public class ArticlePortalServiceImpl extends BaseService implements ArticlePortalService {

    @Autowired
    private ArticleMapper articleMapper;

    /**
     * 首页查询文章列表
     *
     * @param keyword  查询关键词
     * @param category 查询标签(即类型)
     * @param page     分页中的第几页
     * @param pageSize 每页有几条记录
     * @return 分页好的查询结果
     */
    @Override
    public PagedGridResult queryIndexArticleList(String keyword, Integer category, Integer page, Integer pageSize) {
        Example articleExample = new Example(Article.class);
        Example.Criteria criteria = setDefaultArticleExample(articleExample);

        if (StringUtils.isNotBlank(keyword)) {
            criteria.andLike("title", "%"+keyword+"%");
        }
        if (category != null) {
            criteria.andEqualTo("categoryId", category);
        }

        PageHelper.startPage(page, pageSize);
        List<Article> articleList = articleMapper.selectByExample(articleExample);

        return setterPagedGrid(articleList, page);
    }

    /**
     * 查询热新闻列表
     *
     * @return 热新闻列表
     */
    @Override
    public List<Article> queryHotList() {
        Example articleExample = new Example(Article.class);
        Example.Criteria criteria = setDefaultArticleExample(articleExample);

        PageHelper.startPage(1, 5);
        List<Article> articleList = articleMapper.selectByExample(articleExample);

        return articleList;
    }

    /**
     * 设置公共的 Criteria 的隐藏条件 ()
     *          * 查询首页文章的自带隐性查询条件:
     *          *      isAppoint=0 即时发布, 表示文章已经直接发布, 或者已经到了定时发布时候
     *          *      isDelete=0 未逻辑删除
     *          *      articleStatus = 3 通过审核, 文章通过机审或人工审核
     *
     * @param articleExample example
     * @return 设置好查询条件的对应example 的 Example.Criteria
     */
    private Example.Criteria setDefaultArticleExample(Example articleExample) {
        articleExample.orderBy("publishTime").desc();

        /**
         * 查询首页文章的自带隐性查询条件:
         *      isAppoint=0 即时发布, 表示文章已经直接发布, 或者已经到了定时发布时候
         *      isDelete=0 未逻辑删除
         *      articleStatus = 3 通过审核, 文章通过机审或人工审核
         */
        Example.Criteria criteria = articleExample.createCriteria();
        criteria.andEqualTo("isAppoint", YesOrNo.NO.type);
        criteria.andEqualTo("isDelete", YesOrNo.NO.type);
        criteria.andEqualTo("articleStatus", ArticleReviewStatus.SUCCESS.type);

        return criteria;
    }

    /**
     * 作者信息页首页查询文章列表
     *
     * @param writerId 作家id
     * @param page     分页中的第几页
     * @param pageSize 每页有几条记录
     * @return 分页好的查询结果
     */
    @Override
    public PagedGridResult queryArticleListOfWriter(String writerId, Integer page, Integer pageSize) {
        Example articleExample = new Example(Article.class);
        Example.Criteria criteria = setDefaultArticleExample(articleExample);
        criteria.andEqualTo("publishUserId", writerId);

        PageHelper.startPage(page, pageSize);
        List<Article> articleList = articleMapper.selectByExample(articleExample);

        return setterPagedGrid(articleList, page);
    }

    /**
     * 查询作者近期文章列表
     *
     * @param writerId 作家id
     * @return 作者近期文章列表
     */
    @Override
    public PagedGridResult queryGoodArticleListOfWriter(String writerId) {
        Example articleExample = new Example(Article.class);
        articleExample.orderBy("publishTime").desc();
        Example.Criteria criteria = setDefaultArticleExample(articleExample);
        criteria.andEqualTo("publishUserId", writerId);

        PageHelper.startPage(1, 5);
        List<Article> articleList = articleMapper.selectByExample(articleExample);
        return setterPagedGrid(articleList, 1);
    }

    /**
     * 查询文章详情
     * @param articleId 文章ID
     * @return 文章信息
     */
    @Override
    public ArticleDetailVO queryDetail(String articleId) {

        Article article = new Article();
        article.setId(articleId);
        article.setIsAppoint(YesOrNo.NO.type);
        article.setIsDelete(YesOrNo.NO.type);
        article.setArticleStatus(ArticleReviewStatus.SUCCESS.type);

        Article result = articleMapper.selectOne(article);

        ArticleDetailVO detailVO = new ArticleDetailVO();
        BeanUtils.copyProperties(result, detailVO);

        detailVO.setCover(result.getArticleCover());

        return detailVO;
    }
}
